package com.common.app.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import com.common.app.R
import com.common.app.databinding.DialogDrawingBoardSignLayoutBinding
import com.common.base.utils.ViewUtils


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：14/2/2023
 *
 * 描述：通用画板签名弹框
 *
 * 修订历史：
 *
 */
class CurDrawingBoardSignDialog(context: Context) : Dialog(context),
    View.OnClickListener {
    private var binding: DialogDrawingBoardSignLayoutBinding? = null
    private val context: Context

    private var listener: OnDrawingBoardSignListener? = null

    init {
        setDialogTheme()
        setCanceledOnTouchOutside(false)
        this.context = context
    }

    /**
     * set dialog theme(设置对话框主题)
     */
    private fun setDialogTheme() {
        requestWindowFeature(Window.FEATURE_NO_TITLE) // android:windowNoTitle
        window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) // android:backgroundDimEnabled默认是true的
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) // android:windowBackground
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogDrawingBoardSignLayoutBinding.inflate(LayoutInflater.from(getContext()))
        binding?.root?.let { setContentView(it) }
        initView()
        initData()
        initEvent()
    }

    private fun initEvent() {
        binding?.closeImg?.setOnClickListener(this)
        binding?.btnclear?.setOnClickListener(this)
        binding?.btnSave?.setOnClickListener(this)
        binding?.signatureView?.setPaintColor(getContext().getColor(R.color.text_content))
        binding?.signatureView?.setPaintStrokeWidth(10f)
    }

    private fun initView() {
    }

    private fun initData() {
    }
//
//    override fun onBackPressed() {
//        //强制更新时,按返回键不隐藏dialog,方法体置空即可
//    }

    override fun show() {
        //获取当前Activity所在的窗体
        val dialogWindow = window
        //设置Dialog从窗体底部弹出
        dialogWindow?.setGravity(Gravity.CENTER)
        dialogWindow?.decorView?.setPadding(55, 0, 55, 0)
        val lp = dialogWindow?.attributes
        //设置宽
        lp?.width = WindowManager.LayoutParams.MATCH_PARENT
        //设置高
        lp?.height = WindowManager.LayoutParams.WRAP_CONTENT
        dialogWindow?.attributes = lp
        //显示对话框
        dialogWindow?.setGravity(Gravity.CENTER)
        //设置dialog的动画效果
        dialogWindow?.setWindowAnimations(R.style.bottomShowStyle)
        super.show()
    }

    override fun onClick(v: View) {
        //防止多次点击
        if (ViewUtils.isFastClick()) return
        when (v) {
            binding?.closeImg -> {
                dismiss()
            }

            binding?.btnclear -> {
                binding?.signatureView?.clear()
            }

            binding?.btnSave -> {
//                val bmp = binding?.signatureView?.getBitmapFromView()

                binding?.signatureViewTotal?.setBackgroundColor(getContext().getColor(R.color.white))
                listener?.onDrawingBoardSignClick(binding?.signatureView?.getBitmapFromView())
                binding?.signatureViewTotal?.setBackgroundColor(getContext().getColor(R.color.dark_gary))
                dismiss()
            }
        }
    }

    /**
     * @param listener
     */
    fun setListener(listener: OnDrawingBoardSignListener?) {
        this.listener = listener
    }

    /**
     *
     */
    interface OnDrawingBoardSignListener {
        fun onDrawingBoardSignClick(mBmp: Bitmap?)
    }
}